<template>
  <div class="container">
    <div class="form">
      <el-form
        ref="dishForm"
        label-width="100px"
        :model="formModal"
        :rules="rules"
      >
        <el-form-item label="菜品名称" prop="name">
          <el-input v-model="formModal.name" style="width: 300px" />
        </el-form-item>
        <el-form-item label="菜品分类" prop="categoryName">
          <el-select v-model="formModal.categoryName" style="width: 300px">
            <el-option :value="111" />
          </el-select>
        </el-form-item>
        <el-form-item label="菜品价格" prop="price">
          <el-input v-model="formModal.price" style="width: 300px" />
        </el-form-item>
        <el-form-item label="口味做法配置">
          <el-button type="primary">+添加口味</el-button>
        </el-form-item>
        <el-form-item label="菜品图片">
          <el-upload
            class="avatar-uploader"
            :action="`${env}/common/upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="formModal.image"
              :src="`${env}/common/download?name=${formModal.image}`"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item prop="description" label="菜品描述:">
          <el-input
            v-model="formModal.description"
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            style="width: 600px"
          />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-button @click="$router.back()">取消</el-button>
        <el-button type="primary" @click="btnSave">保存</el-button>
        <el-button type="info">保存并继续添加</el-button>
      </el-row>
    </div>
  </div>
</template>

<script>
import { addDish } from "@/api/dish";
export default {
  name: "ListIndex",
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      formModal: {
        name: "",
        price: 0,
        code: "",
        image: "",
        description: "",
        status: 1,
        categoryId: "",
        flavors: [],
      },
      rules: {
        name: [{ required: true, message: "请输入菜品名字", trigger: "blur" }],
        price: [{ required: true, message: "请输入菜品价格", trigger: "blur" }],
        categoryName: [
          { required: true, message: "请输入菜品分类", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleAvatarSuccess(data) {
      this.formModal.image = data.data;
    },
    btnSave() {
      this.$refs.dishForm.validate(async (isok) => {
        if (isok) {
          await addDish(this.formModal);
          this.$message.success("添加菜品成功");
          this.$router.push("/dish");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.form {
  padding: 30px;
}
</style>
